import constant from "@/utils/constant";

export function isEmpty(value) {
  return typeof value === "undefined" || value === null || (typeof value === "string" && value.trim() === "") || (Array.prototype.isPrototypeOf(value) && value.length === 0) || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
}
export function mobile() {
  let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
  return flag && flag.length && flag.length > 0;
}

export function  getDateDiff(dateStr) {
  let publishTime = isNaN(Date.parse(dateStr.replace(/-/gi, "/")) / 1000) ? Date.parse(dateStr) / 1000 : Date.parse(dateStr.replace(/-/gi, "/")) / 1000;
  let d_seconds,
    d_minutes,
    d_hours,
    d_days,
    timeNow = Math.floor(new Date().getTime() / 1000),
    d,
    date = new Date(publishTime * 1000),
    Y = date.getFullYear(),
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
  //小于10的在前面补0
  if (M < 10) {
    M = '0' + M;
  }
  if (D < 10) {
    D = '0' + D;
  }
  if (H < 10) {
    H = '0' + H;
  }
  if (m < 10) {
    m = '0' + m;
  }
  if (s < 10) {
    s = '0' + s;
  }
  d = timeNow - publishTime;
  d_days = Math.floor(d / 86400);
  d_hours = Math.floor(d / 3600);
  d_minutes = Math.floor(d / 60);
  d_seconds = Math.floor(d);
  if (d_days > 0 && d_days < 3) {
    return d_days + '天前';
  } else if (d_days <= 0 && d_hours > 0) {
    return d_hours + '小时前';
  } else if (d_hours <= 0 && d_minutes > 0) {
    return d_minutes + '分钟前';
  } else if (d_seconds < 60) {
    if (d_seconds <= 0) {
      return '刚刚发表';
    } else {
      return d_seconds + '秒前';
    }
  } else if (d_days >= 3) {
    return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
  }
}


/**
 * 表情包转换
 */
export function  faceReg(content) {
  content = content.replace(/\[[^\[^\]]+\]/g, (word) => {
    let index = constant.emojiList.indexOf(word.replace("[", "").replace("]", ""));
    if (index > -1) {
      let url = constant.qiniuDownload + "emoji/q" + (index + 1) + ".gif";
      return '<img style="vertical-align: middle;width: 32px;height: 32px" src="' + url + '" title="' + word + '"/>';
    } else {
      return word;
    }
  });
  return content;
}

/**
 * 图片转换
 */
export function  pictureReg(content) {
  content = content.replace(/\<[^\<^\>]+\>/g, (word) => {
    let index = word.indexOf(",");
    if (index > -1) {
      let arr = word.replace("<", "").replace(">", "").split(",");
      return '<img style="border-radius: 5px;width: 100%;max-width: 250px;display: block" src="' + arr[1] + '" title="' + arr[0] + '"/>';
    } else {
      return word;
    }
  });
  return content;
}
/**
 * 计算两个时间相差的年、月、日、时、分、秒
 *
 * https://juejin.cn/post/7153816601156911118
 */
export function timeDiff(oldTime, newTime) {
  oldTime = oldTime.replace(new RegExp("-", "gm"), "/");
  if (newTime) {
    newTime = newTime.replace(new RegExp("-", "gm"), "/");
  } else {
    newTime = new Date();
  }

  // 计算比较日期
  const getMaxMinDate = (time, twoTime, type) => {
    let minTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? twoTime : time;
    let maxTime = new Date(time).getTime() - new Date(twoTime).getTime() > 0 ? time : twoTime;
    let maxDateDay = new Date(new Date(maxTime).getFullYear(), new Date(maxTime).getMonth() + 1, 0).getDate();
    let maxMinDate = new Date(minTime).getDate() > maxDateDay ? maxDateDay : new Date(minTime).getDate();
    let maxMinTong;
    if (type === 'month') {
      maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(minTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese', {hour12: false});
    } else {
      maxMinTong = new Date(maxTime).getFullYear() + '/' + (new Date(maxTime).getMonth() + 1) + '/' + maxMinDate + ' ' + new Date(minTime).toLocaleTimeString('chinese', {hour12: false});
    }
    return {
      minTime,
      maxTime,
      maxMinTong
    }
  }

  // 相差年份
  const getYear = (time, twoTime) => {
    let oneYear = new Date(time).getFullYear();
    let twoYear = new Date(twoTime).getFullYear();
    const {minTime, maxTime, maxMinTong} = getMaxMinDate(time, twoTime, 'month');
    let chaYear = Math.abs(oneYear - twoYear);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaYear--;
    }
    return chaYear;
  }

  // 相差月份
  const getMonth = (time, twoTime, value) => {
    let oneMonth = new Date(time).getFullYear() * 12 + (new Date(time).getMonth() + 1);
    let twoMonth = new Date(twoTime).getFullYear() * 12 + (new Date(twoTime).getMonth() + 1);
    const {minTime, maxTime, maxMinTong} = getMaxMinDate(time, twoTime, 'day');
    let chaMonth = Math.abs(oneMonth - twoMonth);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      chaMonth--;
    }
    if (value) {
      return chaMonth - value;
    } else {
      return chaMonth;
    }
  }

  // 相差天数
  const getDay = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 86400000) - value;
    } else {
      return parseInt(chaTime / 86400000);
    }
  }

  // 相差小时
  const getHour = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 3600000) - value;
    } else {
      return parseInt(chaTime / 3600000);
    }
  }

  // 相差分钟
  const getMinute = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 60000) - value;
    } else {
      return parseInt(chaTime / 60000);
    }
  }

  // 相差秒
  const getSecond = (time, twoTime, value) => {
    let chaTime = Math.abs(new Date(time).getTime() - new Date(twoTime).getTime());
    if (value) {
      return parseInt(chaTime / 1000) - value;
    } else {
      return parseInt(chaTime / 1000);
    }
  }

  // 相差年月日时分秒
  const getDiffYMDHMS = (time, twoTime) => {
    const {minTime, maxTime, maxMinTong} = getMaxMinDate(time, twoTime, 'day');
    let diffDay1 = getDay(minTime, maxMinTong);
    if (new Date(maxMinTong).getTime() > new Date(maxTime).getTime()) {
      let prevMonth = new Date(maxMinTong).getMonth() - 1;
      let lastTime = new Date(maxMinTong).setMonth(prevMonth);
      diffDay1 = diffDay1 - getDay((new Date(lastTime).getFullYear() + '/' + (new Date(lastTime).getMonth() + 1) + '/' + new Date(lastTime).getDate()), maxMinTong);
    }
    let diffYear = getYear(time, twoTime);
    let diffMonth = getMonth(time, twoTime, diffYear * 12);
    let diffDay = getDay(time, twoTime, diffDay1);
    let diffHour = getHour(time, twoTime, getDay(time, twoTime) * 24);
    let diffMinute = getMinute(time, twoTime, (getDay(time, twoTime) * 24 * 60 + diffHour * 60));
    let diffSecond = getSecond(time, twoTime, (getDay(time, twoTime) * 24 * 60 * 60 + diffHour * 60 * 60 + diffMinute * 60));
    return {
      diffYear,
      diffMonth,
      diffDay,
      diffHour,
      diffMinute,
      diffSecond
    }
  }

  return getDiffYMDHMS(oldTime, newTime);
}
